import commands
from scipy import ceil,arange
import mcbj
from tools.makelatexpdf import makepdf
from mcbj import FindBreakPoints
def AnalyseAll(folder = '091124-jks113-newsetup/14-18/',forcereload=False,findbreakpoints=FindBreakPoints(dGthreashold=0.8,Gthreashold=1.0,GminBreak=0.8),speeds = {},delseries=[],val_to_gapsize={},autoresize=True,resizedata=1.,newpage=True,**kw):
  tf = "results"
  for f in folder.split('/')[:]:
    if len(f)>0:
      tf += "/" + f
      v=commands.getstatusoutput("mkdir %s" % tf)
  species = map(lambda x: x[len("rawdata/%s/scan" % folder):-7],commands.getstatusoutput(("ls -t -r rawdata/%s/scan*.dat.gz" % folder))[1].split("\n"))
  ret={}
  for s in species:
    if (delseries.count(s)>0):
      continue
    size=float(commands.getstatusoutput('ls -l "rawdata/%s/scan%s_%i.dat"  | awk -F" " \'{ print $5 }\'' % (folder,s,100))[1])
    S=mcbj.TUDelftmcbj()
    S.loadfiles(filenames="rawdata/%s/scan%s_*.dat" % (folder,s),findbreakpoints=findbreakpoints,force=forcereload,autoresize=autoresize,resizedata=ceil(size/1.e5)) 
    if speeds.has_key(s):
      S.speed = speeds[s]
    bins = kw.pop('bins',10**arange(-5.5,1,0.04) if float(S.meta['Bias voltage'])<.050 else 10**arange(-6.5,1,0.04))
    S.doReport(folder,bins,s,**kw)
    ret[s] = S.makecomment(newpage=newpage),S.Hbrake.sum(0),S.Hmake.sum(0),S.bins,S.breakpointB,S.breakpointM
    del(S)
  return ret


def plotfolder(folder,forcereload=False,findbreakpoints=FindBreakPoints(dGthreashold=0.6,Gthreashold=1.1,GminBreak=0.1),**kw):
  res = AnalyseAll(folder = folder    ,forcereload=forcereload,findbreakpoints=findbreakpoints,autoresize=True,**kw)
  comments={}
  for k in res:
    comments[k]=res[k][0]
  S=comments.keys()
  S.sort()
  pid=[]
  pid.append(makepdf(S,filename='plotsofall_nosellection',folder="results/%s" % folder, comments=comments, author="Jeppe Fock",title=folder,width='1.1'))
  pid.append(makepdf(S,filename='plotsofall_sellection',folder="results/%s" % folder, comments=comments, author="Jeppe Fock",title=folder,width='0.9',fileends=['_sub_brake']))
  pid.append(makepdf(S,filename='plotsofall_1g0',folder="results/%s" % folder, comments=comments, author="Jeppe Fock",title=folder,width='1.2',fileends=['_g0_brake']))
  pid.append(makepdf(S,filename='plotsofall_corr',folder="results/%s" % folder, comments=comments, author="Jeppe Fock",title=folder,width='1.2',fileends=['_corr_brake']))
  pid.append(makepdf(S,filename='plotolength_brake',folder="results/%s" % folder, comments=comments, author="Jeppe Fock",title=folder,width='1.2',fileends=['_plotolength_brake']))
  pid.append(makepdf(S,filename='All',folder="results/%s" % folder, comments=comments, author="Jeppe Fock",title=folder,width='1.2',filestarts='all_',fileends=['']))
  pid.append(makepdf(S,filename='All2',folder="results/%s" % folder, comments={}, author="Jeppe Fock",title=folder,width='1.1',filestarts='all2_',fileends=['']))
  pid.append(makepdf(S,filename='All3',folder="results/%s" % folder, comments={}, author="Jeppe Fock",title=folder,width='1.1',filestarts='all3_',fileends=['']))
  pid.append(makepdf(S,filename='All4',folder="results/%s" % folder, comments={}, author="Jeppe Fock",title=folder,width='1.1',filestarts=['all2_','all3_'],fileends=['']))
  pid.append(makepdf(S,filename='length_of_1G0_platau',folder="results/%s" % folder, comments=comments, author="Jeppe Fock",title=folder,width='0.5',filestarts=[''],fileends=['_length_of_1G0_platau']))
  pid.append(makepdf(S,filename='timeevolution',folder="results/%s" % folder, comments={}, author="Jeppe Fock",title=folder,width='0.5',filestarts=[''],fileends=['_timeevolution_brake']))
  pid.append(makepdf(S,filename='meanhist',folder="results/%s" % folder, comments={}, author="Jeppe Fock",title=folder,width='1.0',filestarts=[''],fileends=['_meanhists']))  
  pid.append(makepdf(S,filename='matalicwire',folder="results/%s" % folder, comments={}, author="Jeppe Fock",title=folder,width='1.0',filestarts=[''],fileends=['correlate_metallicwire']))  
  del(S)
  return res


